﻿using MoetutuServer.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace MoetutuServer.WebSite.Pixiv
{
    class PixivHelper
    {
        /// <summary>
        ///  判断是否已经下载过，如果没下载返回false 下载过返回true 请注意
        /// </summary>
        /// <param name="Imgurl"></param>
        /// <returns></returns>
        public static bool ReflushLog(int ImgID)
        {

      
            Console.WriteLine(ImgID);
            string TempId = "" + ImgID;
            if (!StaticValue.OldImgID1.Contains(TempId))
            {
                StaticValue.OldImgID1.Add(TempId);

                FileHelper.AppendUTF8Text(StaticValue.OldImgIdPath, TempId);

                return false;

            }
            else
            {
                Console.WriteLine("已下载过，跳过");
                return true;
            }
        }


        /// <summary>
        /// 识别这个画师是否增加了有意义的标签，如果是 原创 漫画这种宽泛意义的标签，这个图不能被人主动检索到，那就不收录
        /// </summary>
        /// <param name="TagList"></param>
        /// <returns></returns>
        public static bool TagIdentify(List<String> TagList)
        {

            int Count = TagList.Count;
            foreach (String line in TagList)
            {
                if (StaticValue.SafeTagList.Contains(line))
                    Count--;
            }

            if (Count == 0)
                return false;
            else
                return true;
        }



        /// <summary>
        /// 选择一个打开网页的方式
        /// </summary>
        /// <param name="Text"></param>
        public static void TagOpenWebMode(String ModeText, String Tag)
        {
            String OpenUrl = "";
            if (ModeText == "P站辅助模式")
            {
                OpenUrl = string.Format("https://www.pixiv.net/search.php?s_mode=s_tag_full&word={0}&order=popular_d", System.Web.HttpUtility.UrlEncode(Tag, Encoding.UTF8));


            }
            else if (ModeText == "百度辅助模式")
            {

                OpenUrl = "https://www.baidu.com/s?wd=" + Tag;
            }
            else
            {
                return;
            }
            System.Diagnostics.Process.Start(OpenUrl);
        }
        /// <summary>
        /// 存放标签的结构体
        /// </summary>
        public struct TagStruct
        {
            String JpTag;
            String CnTag;
            public string JpTag1 { get => JpTag; set => JpTag = value; }
            public string CnTag1 { get => CnTag; set => CnTag = value; }
        }

        /// <summary>
        ///  下载P站文件
        /// </summary>
        /// <param name="Imgurl"></param>
        /// <returns></returns>
        public static string PixivDownload(String Imgurl)
        {

            String Filename = StaticValue.ImgTemp1 + Path.GetFileName(Imgurl);
            using (WebClient DownloadWebClient = new WebClient())
            {

                DownloadWebClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0");
                DownloadWebClient.Headers.Add("Referer", "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=" + Path.GetFileNameWithoutExtension(Imgurl).Replace("_p0", ""));
                //注意，referer这里写死了文件名获取的方法，这里不应该写死的，如需更改请务必注意

                try
                {
                    DownloadWebClient.DownloadFile(new Uri(Imgurl), Filename);
                }
                catch (Exception ex)
                {
                    PrintLog.Log(ex);
                    Filename = "";
                }



            }
            return Filename;

        }


    }
}
